package com.speech.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.speech.R;
import com.speech.adapter.MissionSuperListViewAdapter;
import com.speech.constant.PrefsConstant;
import com.speech.db.SharedPreferenceHelper;
import com.speech.item.MissionSuperListViewItem;
import com.speech.utils.PropertyAndUserInfoHolder;
import com.speech.utils.TitleBuilder;

import java.util.ArrayList;
import java.util.List;

public class MissionSuperActivity extends BaseActivity {

    private ListView lv;

    private SharedPreferenceHelper spf_helper;

    private Integer index;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mission_super);
        _initView();

        _initData();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == index) {
                    startActivity(new Intent(MissionSuperActivity.this,MiningActivity.class));
                    finish();
                }
            }
        });

    }

    private void _initData() {
        spf_helper = new SharedPreferenceHelper(MissionSuperActivity.this, PrefsConstant.FILE_RECORD);

        Integer iCurrentVoiceCount = PropertyAndUserInfoHolder.getTotalReadSentence();

        List<MissionSuperListViewItem> lstData = new ArrayList<>();

        int[] iarr_target = {20, 50, 100, 300, 800, 2000, 5000, 12500};
        int[] iarr_award = {300,500,700,900,1200,1500,1800,2200};
        boolean flag_find_go_finish = false;
        for (int i = 0; i < iarr_target.length; i++) {
            MissionSuperListViewItem item;
            if (iCurrentVoiceCount >= iarr_target[i]) {
                item = new MissionSuperListViewItem(iarr_target[i], iCurrentVoiceCount, iarr_award[i] + "", MissionSuperListViewItem.STATE_DONE);
            } else if (!flag_find_go_finish && iCurrentVoiceCount < iarr_target[i]) {
                item = new MissionSuperListViewItem(iarr_target[i], iCurrentVoiceCount, iarr_award[i] + "", MissionSuperListViewItem.STATE_GO_FINISH);
                flag_find_go_finish = !flag_find_go_finish;
                index = i;
            } else {
                item = new MissionSuperListViewItem(iarr_target[i], iCurrentVoiceCount, "?", MissionSuperListViewItem.STATE_INVISIBLE);
            }
            lstData.add(item);
        }

        MissionSuperListViewAdapter listViewAdapter = new MissionSuperListViewAdapter(lstData, MissionSuperActivity.this);
        lv.setAdapter(listViewAdapter);
    }

    private void _initView() {
        new TitleBuilder(this).setTitleText("超级任务");

        lv = findViewById(R.id.lv_missionSuper);

    }
}
